﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using log4net;
using Xtensive.Storage;
using Xtensive.Storage.Configuration;
using IsolationLevel = System.Transactions.IsolationLevel;

namespace Sherwood.Content.DO
{
    public class SessionProvider : IDataSessionProvider
    {
        private static readonly ILog Log = LogManager.GetLogger(typeof(SessionProvider));

        public SessionProvider(string domainName)
        {
            if(Log.IsDebugEnabled)
                Log.DebugFormat("Building domain {0}", domainName);
            try
            {
                Domain = Domain.Build(DomainConfiguration.Load(domainName));

                if (Log.IsInfoEnabled)
                    Log.InfoFormat("Domain {0} builded", domainName);
            }
            catch(Exception ex)
            {
                Log.Error("Failed to build domain " + domainName, ex);
                throw;
            }
        }

        public Domain Domain { get; private set; }

        public IDataSessionScope CreateScope(IsolationLevel isolationLevel)
        {
            return new SessionScope(this, isolationLevel);
        }

        public IDataSession CreateSession()
        {
            return new DataSession(this);
        }
    }
}
